//JavaScript Framework 2.0 Code
try {
	Type.registerNamespace('com.yonyou.ncmm_pac_pda.BusinessSettingPageController');
	com.yonyou.ncmm_pac_pda.BusinessSettingPageController = function() {
		com.yonyou.ncmm_pac_pda.BusinessSettingPageController.initializeBase(this);
		this.initialize();
	}

	function com$yonyou$ncmm_pac_pda$BusinessSettingPageController$initialize() {

	}

	//UI加载时，如果cache里已经有默认值，则显示出来
	function onPageLoad(sender, args) {
		var stockorg = $cache.read("defaultstockorg") ? $cache.read("defaultstockorg") : "";
		if(stockorg != "") {
			stockorg = $stringToJSON(stockorg);
			var stockvos = NCMMDBUtil.getVOsByPK(StockorgVO, "pk_stockorg", stockorg["pk_stockorg"]);
			var stockvo = null;
			if(stockvos){
				stockvo = stockvos[0];
			}
			if(stockvo) {
				$ctx.put("stockorg", NCMMPubUtil.htmlspecialchars2UI(stockvo["name"]));
				$ctx.dataBind();
			} 
		}

		var stordoc = $cache.read("defaultstordoc") ? $cache.read("defaultstordoc") : "";
		if(stordoc != "") {
			stordoc = $stringToJSON(stordoc);
			var stordocvos = NCMMDBUtil.getVOsByPK(StordocVO, "pk_stordoc", stordoc["pk_stordoc"]); //typeof(stordocvo) == "object"
			var stordocvo = null;
			if(stordocvos){
				stordocvo = stordocvos[0];
			}
			
			if(stordocvo) {
				$ctx.put("stordoc", NCMMPubUtil.htmlspecialchars2UI(stordocvo["name"]));
				$ctx.dataBind();
			} 
		}

	}

	function com$yonyou$ncmm_pac_pda$BusinessSettingPageController$evaljs(js) {
		eval(js)
	}

	// 打开仓库选择界面
	function com$yonyou$ncmm_pac_pda$BusinessSettingPageController$btnstock_onclick(sender, args) {
		var stockorg = $cache.read("defaultstockorg") ? $cache.read("defaultstockorg") : "";
		if(stockorg == "") {
			$alert("请先设置默认库存组织！");
			return;
		}

		//打开指定参照界面
		openRefPage("PDASTORDOC", "stordoc");
	}

	// 打开库存组织选择界面
	function com$yonyou$ncmm_pac_pda$BusinessSettingPageController$btnstockorg_onclick(sender, args) {
		//打开指定参照界面
		openRefPage("PDASTOCKORG", "stockorg");
	}

	function com$yonyou$ncmm_pac_pda$BusinessSettingPageController$btnback_onclick(sender, args) {
		$view.close();
	}

	//保存业务设置并关闭页面
	function onSaveSetting(sender, args) {
		$view.close();
	}

	// 打开参照页面
	function openRefPage(ncbillcode, ctxkey) {
		var jsonparam = {};
		getRefParam(ncbillcode, function(data) {
			jsonparam = data;
			if(jsonparam == null) {
				//			$alert("jsonparam is null!");
				return;
			}
			$view.open({
				viewid: "com.yonyou.ncmm_pac_pda.RefPage", //目标页面
				isKeep: "true", //打开新页面的同时是否保留当前页面，true为保留，false为不保留
				data: jsonparam,
				callback: function() {
					var refdata = $param.getJSONObject("data");

					var queryvos = null;
					var queryvo = null;
					if(ctxkey == "stockorg") {
						queryvos = NCMMDBUtil.getVOsByPK(StockorgVO, "pk_stockorg", refdata["pk_stockorg"]);
						if(queryvos) {
							queryvo = queryvos[0];
//							$alert("queryvo= " + JSON.stringify(queryvo));
						}
					} else {
						queryvos = NCMMDBUtil.getVOsByPK(StordocVO, "pk_stordoc", refdata["pk_stordoc"]);
						if(queryvos) {
							queryvo = queryvos[0];
//							$alert("queryvo= " + JSON.stringify(queryvo));
						}
					}

					if(queryvo) {
						queryvo = NCMMPubUtil.htmlspecialjson2UI(queryvo);
						$ctx.put(ctxkey, queryvo["name"]);
						$ctx.dataBind();
						$cache.write("default" + ctxkey, $jsonToString(queryvo));

						//设置了默认库存组织后，自动下载默认库存组织下的仓库
						//where pk_org='库存组织编码'
						if(ctxkey == "stockorg") {
							var condition = {
								"pk_stock": "='" + queryvo["pk_stockorg"] + "'"
							};
							//							$alert($jsonToString(condition));
							window.uploadTotal = 0;
							window.uploadSum = 0;
							window.uploadSum = window.uploadSum + 1;
							NCMMDBUtil.processDocDataFromNC2017(StordocVO, "仓库", condition, [], 0, function() {
								$cache.write("defaultstordoc", "");
								$ctx.put("stordoc", "");
								$ctx.dataBind();
							});
						}
					}
				}
			});

		});

	}

	//获取参照vo的vo信息
	//reftype： 外系统调用标识
	function getRefParam(reftype, callback) {
		var condition = {
			"ncbillcode": reftype
		};
		NCMMPubUtil.queryVOByConditions(AppmappingheadVO, condition, function(data) {
			var jsondata = {
				bdlistdata: eval(data)
			};
			var reftitle = (jsondata.bdlistdata)[0]["appbillname"];
			if((jsondata.bdlistdata)[0]["downloadnum"] == "" || (jsondata.bdlistdata)[0]["downloadnum"] == "0") {
				$alert("【" + reftitle + "】没有可参照的数据");
				if(callback != null && callback != undefined && typeof(callback) == 'function') {
					callback(null);
					return;
				}
			}
			var reftablename = (jsondata.bdlistdata)[0]["apptablename"]; //本地表名
			var jsonparam = {
				reftype: reftype, //外系统调用标识
				reftitle: reftitle, //元数据显示名称
				reftablename: reftablename //本地表名
			};
			if(callback != null && callback != undefined && typeof(callback) == 'function') {
				callback(jsonparam);
			}
		});
	}

	function onClearBtn(sender, args) {
		//清除缓存
		$cache.write("defaultfactory", "");
		$cache.write("defaultstockorg", "");
		$cache.write("defaultstordoc", "");
		$cache.write("enabledmodule", "");
		//清空仓库本地表记录
		NCMMDBUtil.clearAllOldData(StordocVO, null);
		//更新本地app_mappinghead表中下载记录数和下载时间
		NCMMDBUtil.updateMappingTable(StordocVO, 0, "", "0");
		//更新界面显示
		$ctx.put("factory", "");
		$ctx.put("stockorg", "");
		$ctx.put("stordoc", "");
		$ctx.dataBind();
	}

	function loadmenu(sender, args) {
		NCMMPubMenu.openDropDownList("button0");
	}
	com.yonyou.ncmm_pac_pda.BusinessSettingPageController.prototype = {
		//  wpanelsetting_onload : com$yonyou$ncmm_pac_pda$BusinessSettingPageController$wpanelsetting_onload,
		btnback_onclick: com$yonyou$ncmm_pac_pda$BusinessSettingPageController$btnback_onclick,
		btnstockorg_onclick: com$yonyou$ncmm_pac_pda$BusinessSettingPageController$btnstockorg_onclick,
		btnstock_onclick: com$yonyou$ncmm_pac_pda$BusinessSettingPageController$btnstock_onclick,
		initialize: com$yonyou$ncmm_pac_pda$BusinessSettingPageController$initialize,
		evaljs: com$yonyou$ncmm_pac_pda$BusinessSettingPageController$evaljs
	};
	com.yonyou.ncmm_pac_pda.BusinessSettingPageController.registerClass('com.yonyou.ncmm_pac_pda.BusinessSettingPageController', UMP.UI.Mvc.Controller);
} catch(e) {
	$e(e);
}